﻿using UnityEngine;

namespace HEFramework
{
    public static class DownUISystem
    {
        [EntitySystem]
        public class DownUIAwakeSystem : AwakeSystem<DownUI>
        {
            protected override void Awake(DownUI _self)
            {
                _self.Open();
            }
        }


        [EntitySystem]
        public class DownUIUpdateSystem : UpdateSystem<DownUI>
        {
            protected override void Update(DownUI _self)
            {
                _self.Update();
            }
        }

        [EntitySystem]
        public class DownUIDestroySystem : DestroySystem<DownUI>
        {
            protected override void Destroy(DownUI _self)
            {
                _self.Close();
            }
        }

        public static void Open(this DownUI _self)
        {
            var gc = _self.AddChild<GameObjectComponent>();
            gc.GameObject = GameObject.Instantiate(Resources.Load<GameObject>("2D/Prefabs/UI/DownUI"));
            gc.GameObject.GetComponent<Canvas>().worldCamera = GameObject.Find("UICamera").GetComponent<Camera>();
            _self.UIWindowBehaviour = gc.GameObject.GetComponent<IUIWindowBehaviour>();
            _self.Bind();
            _self.Update(true);
            _self.Run().Coroutine();
        }


        public static void Bind(this DownUI _self)
        {
        }

        public static void Update(this DownUI _self, bool _isNew = false)
        {
            if (_self.State == 0)
            {
            }
            else if (_self.State == 1)
            {
                //更新界面
                string text_message = "TMPText_Message";

                if (AssetBundleDownloader.Instance.State == eAssetBundleDownloadState.Check)
                {
                    _self.UIWindowBehaviour.GetTMPText(text_message).SetText("资源文件检测中...");
                }

                if (AssetBundleDownloader.Instance.State == eAssetBundleDownloadState.DownLoad)
                {
                    _self.UIWindowBehaviour.GetTMPText(text_message).SetText("资源文件加载中...");
                }

                if (AssetBundleDownloader.Instance.State == eAssetBundleDownloadState.Completed)
                {
                    _self.UIWindowBehaviour.GetTMPText(text_message).SetText("资源文件加载完成");
                }
            }
            else if (_self.State == 2)
            {
                World.Instance.DestroyScene(eSceneType.Main);
                World.Instance.CreateScene(eSceneType.Test, 100);
            }
        }

        public static void Close(this DownUI _self)
        {
        }

        private static async HETask Run(this DownUI _self)
        {
            _self.State = 0;
            var tc = _self.GetSceneChild<TimerComponent>();
#if UNITY_EDITOR

#else
            await tc.WaitAsync(3000);
#endif
            _self.State = 1;
            var rc = _self.GetSceneChild<ResourcesComponent>();
            if (rc == null)
            {
                return;
            }

            await rc.UpdateAssetBundleFile();
            rc.LoadAssetBundleDependenciesSyn();
            _self.GetChild<DataTableComponent>().LoadLocalizationDic("Default");
            _self.State = 2;
        }
    }
}